import { defineStore } from "pinia";
import { wgs84togcj02 } from "@/utils/coordinateTrans.js";
import { getCompanyDate, getAirportDate } from "@/api/cockpit/construct";
import { getAirrouteDate } from "@/api/cockpit/airroute";
import { getAirApplyListApi } from "@/api/flightTask/airspaceApply";
import { getDeviceData } from "@/api/cockpit/device";
import { getExecuteNum } from "@/api/cockpit/executeSorties";
import { getAircraftNum } from "@/api/cockpit/planeSorties";
import { getDetectNum } from "@/api/cockpit/detectNum";
import { getFlyPlanNum } from "@/api/cockpit/flyPlan";
import { getTodayAlarm, getTotalAlarm } from "@/api/cockpit/alarm";
import { dataFormat } from "@/utils/format";
import { getAirportWeather } from "@/api/cockpit/weather";
import { airlineFormat } from "@/utils/airlineFromat";

const useCockpitStore = defineStore("cockpit", {
  store: () => ({
    // 企业数据
    companyList: [],
    totalCompany: 0,
    // 机场数据
    mhAirportList: [],
    thAirportList: [],
    czAirportList: [],
    wrjAirportList: [],
    // 航线统计
    totalLine: {},
    currentLine: {},
    toBeUseLine: {},
    // 通航航线数据
    srPlaneLines: [],
    wxPlaneLines: [],
    zsjPlaneLines: [],
    xyjlaneLines: [],
    hxjPlaneLines: [],
    // 无人机航线数据
    ppUAVLines: [],
    zzUAVLines: [],
    // 其它航线数据
    hmAirline: [],
    ftAirline: [],
    rqqAirline: [],
    qqqAirline: [],
    dlsAirline: [],
    // 总航线数据
    totalLines: [],
    // 设备数据
    zhenceList: [],
    wuxiandianList: [],
    weatherDeviceList: [],
    fanzhiList: [],
    youpianList: [],
    // 执行架次统计
    executeData: {},
    // 飞行器架次统计
    aircraftData: {},
    // 今日侦测数量
    deteceNum: 0,
    deteceSorties: {},
    // 今日告警
    todayAlarm: 0,
    todayWarning: {},
    // 告警总数
    totalAlarm: {},
    // 飞行计划执行统计
    willExeNum: {},
    executingNum: {},
    totalFlyPlanNum: {},
    willExeList: [],
    executingList: [],
    airportWeatherList: [],
  }),
  actions: {
    // 查询企业数据
    async getCompanyAsync() {
      const { data } = await getCompanyDate();
      this.totalCompany = data.length;
      this.companyList = data.map((item) => {
        let [longitude, latitude] = wgs84togcj02(item.longitude, item.latitude);
        return {
          ...item,
          id: item.unitId,
          longitude,
          latitude,
          EntityType: "company",
          icon: import.meta.env.VITE_BASE_PUBLICPATH + "/img/cockpit/company.png",
        };
      });
    },

    // 查询企业详情
    getCompanyDetail(id) {
      const data = this.companyList.find((item) => item.id === id);
      return data;
    },

    // 查询机场数据
    async getAirportAsync(type) {
      const { data } = await getAirportDate({ type });
      this.totalAirPort = data.length;
      let list = data.map((item) => {
        let [longitude, latitude] = wgs84togcj02(item.longitude, item.latitude);
        return {
          ...item,
          id: item.name,
          longitude,
          latitude,
          EntityType: type === 0 ? "airport" : "otherAirport",
          icon: import.meta.env.VITE_BASE_PUBLICPATH + `/img/cockpit/airport${type}.png`,
        };
      });
      switch (type) {
        case 0:
          this.mhAirportList = list;
          break;
        case 1:
          this.thAirportList = list;
          break;
        case 2:
          this.czAirportList = list;
          break;
        case 3:
          this.wrjAirportList = list;
          break;
      }
    },

    // 查询机场详情
    getAirportDetail(id) {
      const data = this.airportList.find((item) => item.id === id);
      return data;
    },

    // 查询航线统计数据
    async getAirouteAsync() {
      const res = await getAirrouteDate();
      this.totalLine = res.data;
      const res1 = await getAirrouteDate({ flightRouteStatus: 1 });
      this.currentLine = res1.data;
      const res2 = await getAirrouteDate({ flightRouteStatus: 0 })
      this.toBeUseLine = res2.data;
    },

    // 查询通航航线数据
    async getTHAirline() {
      const { rows } = await getAirApplyListApi(
        {},
        {
          parentAircraftType: 'general_aviation_type',
          flightRouteStatus: 1,
          approvalStatus: 1,
          flightArea: 1,
        }
      );
      let arr = rows.map((item) => {
        return airlineFormat(item, "airline")
      });


      this.srPlaneLines = arr.filter(item => item.aircraftType === 0);
      this.wxPlaneLines = arr.filter(item => item.aircraftType === 1);
      this.zsjPlaneLines = arr.filter(item => item.aircraftType === 2);
      this.xyjPlaneLines = arr.filter(item => item.aircraftType === 3);
      this.hxjPlaneLines = arr.filter(item => item.aircraftType === 4);
      this.totalLines = arr; //所有的数据
    },

    // 查询无人机航线数据
    async getUAVAirline() {
      const { rows } = await getAirApplyListApi(
        {},
        {
          parentAircraftType: 'drone_type',
          flightRouteStatus: 1,
          approvalStatus: 1,
          flightArea: 1,
        }
      );
      let arr = rows.map((item) => {
        return airlineFormat(item, "airline")

      });

      this.ppUAVLines = arr.filter(item => item.aircraftType === 0);
      this.zzUAVLines = arr.filter(item => item.aircraftType === 1);

      this.totalLines = arr; //所有的数据
    },

    // 查询其它航线数据
    async getOtherAirline() {
      const { rows } = await getAirApplyListApi(
        {},
        {
          parentAircraftType: 'other_aircraft_type',
          flightRouteStatus: 1,
          approvalStatus: 1,
          flightArea: 1,
        }
      );
      let arr = rows.map((item) => {
        return airlineFormat(item, "airline")
      });

      this.hmAirline = arr.filter(item => item.aircraftType === 0)
      this.ftAirline = arr.filter(item => item.aircraftType === 1)
      this.rqqAirline = arr.filter(item => item.aircraftType === 2);
      this.qqqAirline = arr.filter(item => item.aircraftType === 3)
      this.dlsAirline = arr.filter(item => item.aircraftType === 4)

      this.totalLines = arr; //所有的数据
    },



    // 查询航线数据详情
    getLineDetail(id) {
      const data = this.totalLines.find((item) => item.airspaceApplyId === id);
      return data;
    },

    // 查询设备列表数据
    async getDeviceList() {
      const { data } = await getDeviceData();
      let fanzhiList = [];
      let zhenceList = [];
      let wuxiandianList = [];
      let weatherDeviceList = [];
      let youpianList = [];
      await data.forEach((item) => {
        let longitude = item.longitude * 1;
        let latitude = item.latitude * 1;
        let typeId = item.typeId;
        switch (typeId) {
          // 无线电探测设备
          case 1:
          case 7:
          case 9:
            wuxiandianList.push({
              ...item,
              EntityType: "device",
              longitude,
              latitude,
              icon: import.meta.env.VITE_BASE_PUBLICPATH + "/img/cockpit/Frame2.png",
            });
            break;
          // 无线电压制设备
          case 4:
          case 5:
            fanzhiList.push({
              ...item,
              EntityType: "device",
              longitude,
              latitude,
              icon: import.meta.env.VITE_BASE_PUBLICPATH + "/img/cockpit/Frame1.png",
            });
            break;
          // 导航诱骗设备
          case 6:
            youpianList.push({
              ...item,
              EntityType: "device",
              longitude,
              latitude,
              icon: import.meta.env.VITE_BASE_PUBLICPATH + "/img/cockpit/Frame5.png",
            });
            break;
          // 无人机探测设备
          case 8:
            zhenceList.push({
              ...item,
              EntityType: "device",
              longitude,
              latitude,
              icon: import.meta.env.VITE_BASE_PUBLICPATH + "/img/cockpit/Frame3.png",
            });
            break;
          // 气象设备
          case 12:
            weatherDeviceList.push({
              ...item,
              EntityType: "device",
              longitude,
              latitude,
              icon: import.meta.env.VITE_BASE_PUBLICPATH + "/img/cockpit/Frame4.png",
            });
            break;
        }
      });
      this.fanzhiList = fanzhiList;
      this.zhenceList = zhenceList;
      this.weatherDeviceList = weatherDeviceList;
      this.youpianList = youpianList;
      this.wuxiandianList = wuxiandianList;
    },

    // 查询执行架次统计
    async getExecuteData(type = 'total') {
      const { data } = await getExecuteNum({ type });
      this.executeData = data;
    },

    // 查询飞行器统计
    async getAircraftData() {
      const { data } = await getAircraftNum();
      this.aircraftData = data;
    },

    // 查询侦测统计
    async getDetectData() {
      this.deteceNum = 0;
      const { data } = await getDetectNum();
      this.deteceNum = `${data.sum}`;
      this.deteceSorties = data;
    },

    // 查询飞行计划执行状态数量
    async getFlyPlanData() {
      const res = await getFlyPlanNum({});
      this.totalFlyPlanNum = res.data;
      setTimeout(async () => {
        const res1 = await getFlyPlanNum({ executionStatus: 1 });
        this.executingNum = res1.data;
      }, 700)
      setTimeout(async () => {
        const res2 = await getFlyPlanNum({ executionStatus: 0 });
        this.willExeNum = res2.data;
      }, 1400)
    },

    // 查询告警统计数据
    async getAlarmNum() {
      const { data } = await getTotalAlarm({ type: "total" });
      this.totalAlarm = data;
    },
    // 查询今日告警统计数据
    async getTodayAlarmNum() {
      const { data } = await getTodayAlarm();
      this.todayAlarm = `${data.totalAlarmsNum}`;
      this.todayWarning = data;
    },

    // 查询机场的天气
    getAirportWeatherFun() {
      let weather = [];
      let list = ["建德千岛湖通用机场", "杭州萧山国际机场", "杭州笕桥机场"];
      list.forEach(async (item) => {
        const { data } = await getAirportWeather(item);
        let obj = {
          ...data,
          label: item,
        };
        weather.push(obj);
        window.airportWeatherList = weather;
      });
      this.airportWeatherList = weather;
    },
  },
});

export default useCockpitStore;
